import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

public class OutputChanels {

	private Map<String, ObjectOutputStream> outs = new HashMap<String, ObjectOutputStream>();

	public synchronized void addOut(String name, ObjectOutputStream out) throws IOException {
		outs.put(name, out);
		sendUserList();
	}

	private void sendUserList() throws IOException {
		broadcastMessage(new UserListMessage("System", 
				outs.keySet().toArray(new String[0])));
	}

	public synchronized void broadcastMessage(Message message)
			throws IOException {
		for (ObjectOutputStream out : outs.values()) {
			out.writeObject(message);
		}
	}

	public synchronized void remove(String name) throws IOException {
		outs.remove(name);
		broadcastMessage(new BroadcastMessage(name,
				" foi embora!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
		sendUserList();
	}

}
